import { defineStore } from "pinia";

import {postax,getax,deleteax} from '@/common/request/index'

import { UseAuthCode } from "./utils";

export const UserCollectStore = defineStore('UserClollectStore', {

    state: () => {
        return {
            // 用户所有收藏
            user_collect: {},
            // 获取用户收藏请求配置
            user_collect_option: {
                pageNo: 1,
                pageSize:10,
                userId:null,
            },
            // 添加删除 请求配置
            add_delete_collect: {
                animeId:null,
                userId: null,
            },
            // 是否收藏
            isCollect: null,
        }
    },
    getters: {
        
    },
    actions: {
        // 获取用户所有收藏
        async getUserCollect() {
            if (this.user_collect_option.userId === null) {
                await getax('api/index/info').then((response) => {
                    this.user_collect_option.userId = response.data.data.userId
                })
            }
            const {data:{data:res}} = await postax('api/userCollect/getCollects',this.user_collect_option)
            this.user_collect = res
        },
        // 添加收藏
        async addCollect() {
            if (this.add_delete_collect.userId === null) {
                alert('添加失败')
                return
            }
            await this.queryCollect(this.add_delete_collect, (res) => {
                if (res.data === this.isCollect) return
            })
            console.log(this.add_delete_collect)
            await postax('api/userCollect/save',this.add_delete_collect).then((response) => {
                console.log("添加成功")
            })
        },
        // 取消收藏
        async deleteCollect() {
            await this.queryCollect(this.add_delete_collect, (res) => {
                if (res.data === this.isCollect) return
            })
            await deleteax(`api/userCollect/delete/${this.add_delete_collect.userId}/${this.add_delete_collect.animeId}`).then((response) => {
                console.log('删除成功')
            })
            
        },
        // 查看动漫是否存在
        async queryCollect({userId,animeId},callback) {
            if (this.userId === null) {
                await getax('api/index/info').then((response) => {
                    userId = response.data.data.userId
                })
            }
            const {data:res} = await getax(`api/userCollect/isExistAnimeCollect/${userId}/${animeId}`)
            callback(res)
        }
    }
})

export const UseLoginStore = defineStore('LoginStore', {
    state: () => {
        return {
            // 登录配置
            LoginOption: {
                userNameOrEmail: '',
                password:''
            },
            // 短信验证配置
            SendOption: {
                name: '',
                sendTo:''
            },
            VerifyOption: {
               code:'' 
            },
            // 注册配置
            SignInOption: {
                code:0,
                email: '',
                password: '',
                userName: '',
            },
            // 忘记密码配置
            forgetOption: {
                admin: '',
                password:'',
            },
            AuthCode : UseAuthCode()
        }
    },
    getters: {
        
    },
    actions: {
        async Login(callback) {
            if (this.AuthCode.yanzhen !== this.AuthCode.true_code) return alert('验证码错误')
            const {data:res} = await postax('api/index/login',this.LoginOption)

            try {
                if (res.data.token && res.code === 200) {
                    localStorage.setItem('token',res.data.token)

                    window.location.reload()
                    setTimeout(() => {
                        window.location.href = '#/home'
                    },200)
                    return
                }
            } catch {
                callback(res.code)
                this.AuthCode.handleCanvas()
            }
            
            
        },
        async SendEmail() {
            const reg = /^([a-zA-Z\d][\w-]{2,})@(\w{2,})\.([a-z]{2,})(\.[a-z]{2,})?$/
            if (!reg.test(this.SendOption.sendTo)) {
                alert('错误邮箱')
                return
            }
            const {data:res} = await postax(`api/sendMail/sendVerificationCodeMail/${this.SendOption.name}/${this.SendOption.sendTo}`)
            if (res.code === 200) return
        },
        async SignIn() {
            this.SignInOption.code = parseInt(this.AuthCode.yanzhen)
            const {data:res} = await postax('api/index/register',this.SignInOption)
            if (res.code === 200) {
                alert('注册成功')
                location.reload()
            }
            else {
                alert(`${res.msg}`)
            }
        },
        async ForgetPassword(callback) {}
    }
})